global input "Q:\dc1prhcmsas01\PU2\data - sas"
global temp "Q:\dc1prhcmsas01\PU2\temp_stata_wr"
global log "Q:\dc1prhcmsas01\PU2\Log - Stata WR"
global output "Q:\dc1prhcmsas01\PU2\data_stata_wr"
global graphs "Q:\dc1prhcmsas01\PU2\graphs_stata_wr"
global tables "Q:\dc1prhcmsas01\PU2\tables_stata_wr"

cd "Q:\dc1prhcmsas01\PU2\Code - Stata WR"

set scheme plotplain
graph set window fontface "Times New Roman"

capture log close
log using "$log/17_firm_employment_binary", replace

****************************************
*Define share of firm treated 
****************************************
	use client_id base0 salaried0 state yr_month if yr_month==mdy(4,1,2016) using "$output\unbalanced_panel_treat.dta", clear
		gen treat_sal=salaried0==1 & base0>=455 & base0<=913
		egen state_num=group(state)
		egen mode_state=mode(state_num), by(client_id)
			
	collapse (mean) treat_sal mode_state, by(client_id)
	egen p33 = pctile(treat_sal), p(33) 
	egen p66 = pctile(treat_sal), p(66)
	gen tercile1=treat_sal<=p33 
	gen tercile2=treat_sal>p33 & treat_sal<=p66 
	gen tercile3=treat_sal>p66
	gen tercile=1 if tercile1==1
	replace tercile=2 if tercile2==1
	replace tercile=3 if tercile3==1
	egen p50 = median(treat_sal) 
	gen median = treat_sal>p50
	tab tercile, sum(treat_sal)
	save "$temp/baseline_treated2", replace
	
	
****************************************
*Merge onto panel 
****************************************
	use "$output\unbalanced_distribution_treat.dta", clear
	gen bunched= n if bin==913 & salaried==1
	replace bunched = 0 if bunched==.
	gen n_salaried= n if salaried==1 
	gen base_salaried = base if salaried==1 
	
	*Collapse by firm-year
	gcollapse (sum) n base bunched n_salaried base_salaried, by(yr_month client_id)
	gen log_n=log(n)
	gen log_n_sal=log(n_salaried)
	gen log_n_hr=log(n-n_salaried)
	gen wage_bill=base/n
	gen log_bill=log(wage_bill)
	gen wage_bill_sal=base_salaried/n_salaried 
	gen log_bill_sal=log(wage_bill_sal)
	gen log_bill_hr=log((base-base_salaried)/(n-n_salaried))
	gen time=mofd(yr_month)-mofd(mdy(4,01,2016))
	merge m:1 client_id using "$temp/baseline_treated2"
	drop _merge
	merge m:1 client_id using "$output/client_crosswalk"
	drop _merge
	
	
****************************************
*Program to plot raw means
****************************************
cap program drop raw_plot
program define raw_plot 
args var ytitle pos ylabel

	preserve 
		sum p33 
		local p33 = round(`r(mean)',0.01)
		sum p66
		local p66 = round(`r(mean)',0.01)
		collapse (mean) `var', by(tercile time)

			label define time_lab -12 "Apr 2015" -11 "May 2015" -10 "Jun 2015" ///
			-9 "Jul 2015" -8 "Aug 2015" -7 "Sep 2015" -6 "Oct 2015" -5 "Nov 2015" -4 "Dec 2015" ///
			-3 "Jan 2016" -2 "Feb 2016" -1 "Mar 2016" 0 "Apr 2016" 1 "May 2016" 2 "Jun 2016" ///
			3 "Jul 2016" 4 "Aug 2016" 5 "Sep 2016" 6 "Oct 2016" 7 "Nov 2016" 8 "Dec 2016" ///
			9 "Jan 2017" 10 "Feb 2017" 11 "Mar 2017" 12 "Apr 2017" 13 "May 2017" 14 "Jun 2017" ///
			15 "Jul 2017" 16 "Aug 2017" 17 "Sep 2017" 18 "Oct 2017" 19 "Nov 2017" 20 "Dec 2017" ///
			21 "Jan 2018" 22 "Feb 2018" 23 "Mar 2018" 24 "Apr 2018" 25 "May 2018", replace
		label values time time_lab 
		
		graph twoway (connect `var' time if tercile==1, lc(black) mcolor(black)) ///
			(connect `var' time if tercile==2, lc(maroon) mcolor(maroon) msymbol(D)) /// 
			(connect `var' time if tercile==3) , ///
			xline(1 8, lcolor(black)) xtitle("Month") xlabel(-12(3)25, valuelabel angle(45)) ///
			ytitle("`ytitle'") ylabel(`ylabel') legend(order(1 "< `p33' " 2 "> `p33' & < `p66'" 3 "> `p66'") subtitle("Share of Workers Exposed") ring(0) position(`pos') col(2) region(lstyle(solid) lcolor(black) lwidth(thin)))
		graph export "$graphs/exposure_`var'_raw.eps", replace
		graph export "$graphs/exposure_`var'_raw.jpg", replace
	restore 
end 
raw_plot log_n "Log Number of Workers" 3
raw_plot log_n_sal "Log Number of Salaried Workers" 3
raw_plot log_n_hr "Log Number of Hourly Workers" 4 2.75(0.5)4.5
raw_plot log_bill "Log Avg. Wage Bill per Worker" 4
raw_plot log_bill_sal "Log Avg. Wage Bill of Salaried Workers" 4 6.75(0.5)7.75
raw_plot log_bill_hr "Log Avg. Wage Bill of Hourly Workers" 4
	

****************************************
*Program to estimate regressions
****************************************
cap program drop reg_est
program define reg_est 
args var name

	*Estimate diff-in-diff
	preserve 
		rename mode_state state
		
		local index=1
		forvalues i=-11/25 {
			gen d_`index'= (tercile3==1)*time==`i'
			gen g_`index'= (tercile2==1)*time==`i'
			label var d_`index' "Treat=1 and Event Time=`i'"
			local ++index
		}
		drop d_12
		drop g_12
		gen treatpost1 =  (tercile3==1)*(time>8)
		gen treatpost2 =  (tercile3==1)*(time>0 & time<=8)
		gen treatpost3 =  (tercile2==1)*(time>8)
		gen treatpost4 =  (tercile2==1)*(time>0 & time<=8)
		
		*********************
		*Baseline regression
		*********************
		reghdfe `var' d_* g_*, absorb(client_id time) vce(cluster client_id)
		
		cap drop beta ub lb
		gen event_time=.
		gen beta=.
		gen ub=.
		gen lb=.
		gen beta2=.
		gen ub2=.
		gen lb2=.
		gen spec=""
		gen se=.
		gen obs =.
				
		local index=1
		forvalues i=-11/25 {
			local j=`i'+12
			replace event_time=`i' if _n==`index'
			replace spec="Basic" if _n==`index'
			if `i'!=0 {
				replace beta=_b[d_`j'] if _n==`index'
				replace ub=_b[d_`j'] + 1.96*_se[d_`j'] if _n==`index'
				replace lb=_b[d_`j'] - 1.96*_se[d_`j'] if _n==`index'	
				replace beta2=_b[g_`j'] if _n==`index'
				replace ub2=_b[g_`j'] + 1.96*_se[g_`j'] if _n==`index'
				replace lb2=_b[g_`j'] - 1.96*_se[g_`j'] if _n==`index'	
			}
			if `i'==0 {
				replace beta=0 if _n==`index'
				replace ub=0 if _n==`index'
				replace lb=0 if _n==`index'
				replace beta2=0 if _n==`index'
				replace ub2=0 if _n==`index'
				replace lb2=0 if _n==`index'
			}
			local ++index
		}
		reghdfe `var' treatpost1 treatpost2 treatpost3 treatpost4, absorb(client_id time) vce(cluster client_id)
			replace spec="Basic1p66" if _n==`index'
			replace beta=_b[treatpost1] if _n==`index'
			replace se=_se[treatpost1] if _n==`index'
			replace obs=`e(N)' if _n==`index'
			local ++index
			replace spec="Basic2p66" if _n==`index'
			replace beta=_b[treatpost2] if _n==`index'
			replace se=_se[treatpost2] if _n==`index'
			replace obs=`e(N)' if _n==`index'
			local ++index
			replace spec="Basic1p33" if _n==`index'
			replace beta=_b[treatpost3] if _n==`index'
			replace se=_se[treatpost3] if _n==`index'
			replace obs=`e(N)' if _n==`index'
			local ++index
			replace spec="Basic2p33" if _n==`index'
			replace beta=_b[treatpost4] if _n==`index'
			replace se=_se[treatpost4] if _n==`index'
			replace obs=`e(N)' if _n==`index'
			local ++index
		
		*********************
		*State FE regression
		*********************
		reghdfe `var' d_* g_*, absorb(state##client_id state##time) vce(cluster client_id)
			
		forvalues i=-11/25 {
			local j=`i'+12
			replace event_time=`i' if _n==`index'
			replace spec="State_FE" if _n==`index'
			if `i'!=0 {
				replace beta=_b[d_`j'] if _n==`index'
				replace ub=_b[d_`j'] + 1.96*_se[d_`j'] if _n==`index'
				replace lb=_b[d_`j'] - 1.96*_se[d_`j'] if _n==`index'	
				replace beta2=_b[g_`j'] if _n==`index'
				replace ub2=_b[g_`j'] + 1.96*_se[g_`j'] if _n==`index'
				replace lb2=_b[g_`j'] - 1.96*_se[g_`j'] if _n==`index'	
			}
			if `i'==0 {
				replace beta=0 if _n==`index'
				replace ub=0 if _n==`index'
				replace lb=0 if _n==`index'
				replace beta2=0 if _n==`index'
				replace ub2=0 if _n==`index'
				replace lb2=0 if _n==`index'
			}
			local ++index
		}
		
		reghdfe `var' treatpost1 treatpost2 treatpost3 treatpost4, absorb(state##client_id state##time) vce(cluster client_id)
			replace spec="State1p66" if _n==`index'
			replace beta=_b[treatpost1] if _n==`index'
			replace se=_se[treatpost1] if _n==`index'
			replace obs=`e(N)' if _n==`index'
			local ++index
			replace spec="State2p66" if _n==`index'
			replace beta=_b[treatpost2] if _n==`index'
			replace se=_se[treatpost2] if _n==`index'
			replace obs=`e(N)' if _n==`index'
			local ++index
			replace spec="State1p33" if _n==`index'
			replace beta=_b[treatpost3] if _n==`index'
			replace se=_se[treatpost3] if _n==`index'
			replace obs=`e(N)' if _n==`index'
			local ++index
			replace spec="State2p33" if _n==`index'
			replace beta=_b[treatpost4] if _n==`index'
			replace se=_se[treatpost4] if _n==`index'
			replace obs=`e(N)' if _n==`index'
			local ++index

		
		*********************
		*State NAICS regression
		*********************
		reghdfe `var' d_* g_*, absorb(naics##state##client_id naics##state##time) vce(cluster client_id)
			
		forvalues i=-11/25 {
			local j=`i'+12
			replace event_time=`i' if _n==`index'
			replace spec="NAICS_FE" if _n==`index'
			if `i'!=0 {
				replace beta=_b[d_`j'] if _n==`index'
				replace ub=_b[d_`j'] + 1.96*_se[d_`j'] if _n==`index'
				replace lb=_b[d_`j'] - 1.96*_se[d_`j'] if _n==`index'	
				replace beta2=_b[g_`j'] if _n==`index'
				replace ub2=_b[g_`j'] + 1.96*_se[g_`j'] if _n==`index'
				replace lb2=_b[g_`j'] - 1.96*_se[g_`j'] if _n==`index'	
			}
			if `i'==0 {
				replace beta=0 if _n==`index'
				replace ub=0 if _n==`index'
				replace lb=0 if _n==`index'
				replace beta2=0 if _n==`index'
				replace ub2=0 if _n==`index'
				replace lb2=0 if _n==`index'
			}
			local ++index
		}
		
		reghdfe `var' treatpost1 treatpost2 treatpost3 treatpost4, absorb(naics##state##client_id naics##state##time) vce(cluster client_id)
			replace spec="Naics1p66" if _n==`index'
			replace beta=_b[treatpost1] if _n==`index'
			replace se=_se[treatpost1] if _n==`index'
			replace obs=`e(N)' if _n==`index'
			local ++index
			replace spec="Naics2p66" if _n==`index'
			replace beta=_b[treatpost2] if _n==`index'
			replace se=_se[treatpost2] if _n==`index'
			replace obs=`e(N)' if _n==`index'
			local ++index
			replace spec="Naics1p33" if _n==`index'
			replace beta=_b[treatpost3] if _n==`index'
			replace se=_se[treatpost3] if _n==`index'
			replace obs=`e(N)' if _n==`index'
			local ++index
			replace spec="Naics2p33" if _n==`index'
			replace beta=_b[treatpost4] if _n==`index'
			replace se=_se[treatpost4] if _n==`index'
			replace obs=`e(N)' if _n==`index'
			local ++index
			
		keep if beta!=.
		keep event_time beta* se* ub* lb* spec obs
		
		*Save estimates
		save "$output/explosure_`var'_estimates.dta", replace
	restore
end
reg_est log_n "Log Number of Workers" 
reg_est log_n_sal "Log Number of Salaried Workers" 
reg_est log_n_hr "Log Number of Hourly Workers" 
reg_est log_bill "Log Avg. Wage Bill per Worker"
reg_est log_bill_sal "Log Avg. Wage Bill of Salaried Workers"
reg_est log_bill_hr "Log Avg. Wage Bill of Hourly Workers"
	
log close
	